package com.example.demo.controller;

import com.example.demo.entity.User;
import com.mongodb.client.result.DeleteResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/CollectionDeleteController")
@Api(tags = "集合删除操作")
public class CollectionDeleteController {
    @Autowired
    private MongoTemplate mongoTemplate;

    @ApiOperation(value = "删除集合")
    @GetMapping("delete") 
    public Boolean deleteUser(){
        //删除所有
         DeleteResult remove = mongoTemplate.remove(new Query(), User.class);

         //条件删除
         DeleteResult deleteResult = mongoTemplate.remove(Query.query(Criteria.where("name").is("string")), User.class);
        return deleteResult.wasAcknowledged();
    }
}
